//
// File  : FlightStatusController.cpp
// Team  : Dangling Pointers
// Class : CST 236 - Software Testing
//
// FlightStatusController definitions.
//

#include "Global.h"
#include "FlightStatusController.h"
#include <iostream>

// constructor
FlightStatusController::FlightStatusController(FlightDatabase* flightdatabase) 
	: m_flightdatabase(flightdatabase)
{
	displayMenu();
}

//
// displayMenu : void
//
// method will display the menu for the CustomerProfile and then prompt for user
// input corresponding to menu choice.
//
void FlightStatusController::displayMenu()
{
	while (true) 
	{
		clearConsole();

		std::string menuName = "Flight Status Menu";
		std::cout << std::endl << "====== " << menuName << " ======" << std::endl;
		std::cout << "1. Arrival & Departure Times." << std::endl;
		std::cout << "2. Number of Passengers." << std::endl;
		std::cout << "3. Print Flight Manifest." << std::endl;
		std::cout << "4. Main Menu." << std::endl;

		int input = getUserInput(4);

		switch(input)
		{
		case 1:
			clearConsole();
			arrivalDepartureTimes();
			pressEnterToContinue();
			break;

		case 2:
			clearConsole();
			numberOfPassengers();
			pressEnterToContinue();
			break;

		case 3:
			clearConsole();
			printFlightManifest();
			pressEnterToContinue();
			break;

		default:
			return;
		}
	}
}

//
// arrivalDepartureTimes: void
//
// method will display Arrival & Departure times
//
void FlightStatusController::arrivalDepartureTimes()
{
	std::cout << std::endl << "====== Arrival/Departure Times ======" << std::endl;

	// get flight number
	std::string flightNumber;

	while (true) 
	{
		getFlightFromUser(flightNumber);

		// Enter nothing to break from this menu
		if (flightNumber.size() == 0)
			return;

		bool finished;

		try 
		{
			m_flightdatabase->getFlight(flightNumber);
			finished = false;
		} 
		catch (std::exception) 
		{
			std::cout << "Flight not found, please re-enter (or enter nothing to cancel)" << std::endl;
			continue;
		}

		//no exception thrown, finished changed to true, flight object is grabbed
		if (finished == false)
		{
			finished = true;
		}
		if ( validateFlightNumber(flightNumber) ) 
		{
			// Print out arrival and departure times for flightNumber
			std::cout << "Arrival: " << m_flightdatabase->getFlight(flightNumber).getArrivalTime() << std::endl;
			std::cout << "Departure: " << m_flightdatabase->getFlight(flightNumber).getDepartureTime() << std::endl;

			break;
		}


		std::cout << "Invalid entry! ";
	}
}

//
// numberOfPassengers : void
//
// method will provide number of passengers on a flight given a flight #
// 
void FlightStatusController::numberOfPassengers()
{
	// get flight number
	std::string flightNumber;

	while (true) 
	{
		getFlightFromUser(flightNumber);

		// Enter nothing to break from this menu
		if (flightNumber.size() == 0)
			return;

		bool finished;

		try 
		{
			m_flightdatabase->getFlight(flightNumber);
			finished = false;
		} 
		catch (std::exception) 
		{
			std::cout << "Flight not found, please re-enter (or enter nothing to cancel)" << std::endl;
			continue;
		}

		//no exception thrown, finished changed to true, flight object is grabbed
		if (finished == false)
		{
			finished = true;
		}
		if ( validateFlightNumber(flightNumber) ) 
		{
			std::cout << "======Number of Passengers======" << std::endl;
			std::cout << m_flightdatabase->getFlight(flightNumber).getNumPassengers() << std::endl;
			std::cout << std::endl;
			break;
		}


		std::cout << "Invalid entry! ";
	}
}

//
// printFlightManifest : void
//
// method will print the flight manifest given a flight #
//
void FlightStatusController::printFlightManifest()
{
	std::cout << std::endl << "====== Arrival/Departure Times ======" << std::endl;

	// get flight number
	std::string flightNumber;

	while (true) 
	{
		getFlightFromUser(flightNumber);

		// Enter nothing to break from this menu
		if (flightNumber.size() == 0)
			return;

		bool finished;

		try 
		{
			m_flightdatabase->getFlight(flightNumber);
			finished = false;
		} 
		catch (std::exception) 
		{
			std::cout << "Flight not found, please re-enter (or enter nothing to cancel)" << std::endl;
			continue;
		}

		//no exception thrown, finished changed to true, flight object is grabbed
		if (finished == false)
		{
			finished = true;
		}
		if ( validateFlightNumber(flightNumber) ) 
		{
			// Print flight manifest
			std::cout << std::endl;
			m_flightdatabase->getFlight(flightNumber).printFirstClassSeats();
			std::cout << std::endl;
			m_flightdatabase->getFlight(flightNumber).printCoachClassSeats();
			std::cout << std::endl;

			break;
		}


		std::cout << "Invalid entry! ";
	}
}